<?php
// +----------------------------------------------------------------------
// | 74CMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.74cms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 
// +----------------------------------------------------------------------
// | ModelName: 二维码生成
// +----------------------------------------------------------------------
namespace Api\Controller;
use Common\Controller\ApiController;
class QrcodeController extends ApiController {
	/**
	 * [index 跟据传值，生成二维码]
	 */
	public function index(){
		if($url = I('get.url','','trim')){
            ob_clean();
            $url = htmlspecialchars_decode($url,ENT_QUOTES);
            $download = I('get.download',0,'intval');
            Vendor('phpqrcode.phpqrcode');   
            $qrcode = new \QRcode();
            ob_clean();
            if($download==1){
                header("Content-type:application/x-png");
                header("Content-Disposition:attachment;filename=二维码.png");
                echo $qrcode::png($url,false, 'H', 8, 2);
            }else{
                $qrcode::png($url,false, 'H', 8, 2);

            }
		}
	}

    /**
     * [indexBase64 跟据传值，生成二维码base64]
     */
    public function indexBase64(){
        if($url = I('post.url','','trim')){
            ob_clean();
            $url = htmlspecialchars_decode($url,ENT_QUOTES);
            $download = I('get.download',0,'intval');
            Vendor('phpqrcode.phpqrcode');
            $qrcode = new \QRcode();
            ob_clean();
            if($download==1){
                header("Content-type:application/x-png");
                header("Content-Disposition:attachment;filename=二维码.png");
                echo $qrcode::png($url,false, 'H', 8, 2);
            }else{

                $imageFile  = qrcode($url);
                /*$base64_image = '';
                $image_info = getimagesize($imageFile);
                $image_data = fread(fopen($imageFile, 'r'), filesize($imageFile));
                $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));*/
                $str = "data:image/jpg;base64,".base64_encode($imageFile);
                $this->ajaxReturn(0, 'ok', $str);

                //打开缓冲区
                /*ob_start();
                //生成二维码图片
                $returnData = $qrcode::pngString($url,false, "H", 3, 1);
                //这里就是把生成的图片流从缓冲区保存到内存对象上，使用base64_encode变成编码字符串，通过json返回给页面。
                $imageString = base64_encode(ob_get_contents());
                //关闭缓冲区
                ob_end_clean();
                $str = "data:image/png;base64,".$imageString;
                echo  $str;*/
            }
        }
        $this->ajaxReturn(1, '获取二维码失败');
    }

    public function get_font_img(){
        $str = I('request.str','','trim');
        $str = decrypt($str,C('PWDHASH'));
        \Common\ORG\Image::buildString($str,array(100,50),'','png',0,false);
    }
}
?>